@import "../error-message/index";
@import "../fieldset/index";
@import "../hint/index";
@import "../label/index";

@include govuk-exports("govuk/component/checkboxes") {
  $govuk-touch-target-gutter: 4px;
  $govuk-checkboxes-size: 40px;
  $govuk-touch-target-size: ($govuk-checkboxes-size + $govuk-touch-target-gutter);
  $govuk-small-checkboxes-size: 24px;
  $govuk-checkboxes-label-padding-left-right: govuk-spacing(3);
  $govuk-checkbox-check-horizontal-position: 10px;

  .govuk-checkboxes__item {
    display: flex;
    flex-wrap: wrap;
    position: relative;
    margin-bottom: govuk-spacing(2);
  }

  .govuk-checkboxes__item:last-child,
  .govuk-checkboxes__item:last-of-type {
    margin-bottom: 0;
  }

  .govuk-checkboxes__input {
    // Allow the input to sit above the label, enabling its proper detection
    // when exploring by touch or using automation tools like Selenium
    z-index: 1;
    width: $govuk-touch-target-size;
    height: $govuk-touch-target-size;
    margin: 0;
    opacity: 0;
    cursor: pointer;
  }

  .govuk-checkboxes__label {
    align-self: center;

    // Ensure that the width of the label is never more than the width of the
    // container minus the input width minus the padding on either side of
    // the label. This prevents the label from going onto the next line due to
    // __item using flex-wrap because we want hints on a separate line.
    max-width: calc(100% - #{(($govuk-checkboxes-label-padding-left-right * 2) + $govuk-touch-target-size)});
    margin-bottom: 0;
    padding: (govuk-spacing(1) + $govuk-border-width-form-element) govuk-spacing(3);
    cursor: pointer;
    // remove 300ms pause on mobile
    touch-action: manipulation;
  }

  // [ ] Check box
  .govuk-checkboxes__label::before {
    content: "";
    box-sizing: border-box;
    position: absolute;
    top: ($govuk-touch-target-gutter / 2);
    left: ($govuk-touch-target-gutter / 2);
    width: $govuk-checkboxes-size;
    height: $govuk-checkboxes-size;
    border: $govuk-border-width-form-element solid currentcolor;
    background: transparent;
  }

  // ✔ Check mark
  //
  // The check mark is a box with a border on the left and bottom side (└──),
  // rotated 45 degrees
  .govuk-checkboxes__label::after {
    content: "";
    box-sizing: border-box;
    position: absolute;

    // Use "magic numbers" to define shape and position of check mark because
    // the complexity of the shape makes it difficult to calculate dynamically.
    top: 13px;
    left: $govuk-checkbox-check-horizontal-position;
    width: 23px;
    height: 12px;
    transform: rotate(-45deg);
    border: solid;
    border-width: 0 0 5px 5px;
    // Fix bug in IE11 caused by transform rotate (-45deg).
    // See: alphagov/govuk_elements/issues/518
    border-top-color: transparent;
    opacity: 0;
    background: transparent;
  }

  .govuk-checkboxes__hint {
    display: block;
    width: 100%;
    margin-top: govuk-spacing(-1);
    padding-right: $govuk-checkboxes-label-padding-left-right;
    padding-left: ($govuk-checkboxes-label-padding-left-right + $govuk-touch-target-size);
  }

  // This is to bypass govuk-hint's specificity on hints following labels having
  // a margin bottom of 10px (govuk-spacing(2)). Because checkboxes are flexbox,
  // the margin doesn't collapse so we have to do this manually.
  .govuk-label:not(.govuk-label--m):not(.govuk-label--l):not(.govuk-label--xl) + .govuk-checkboxes__hint {
    margin-bottom: 0;
  }

  // Focused state
  .govuk-checkboxes__input:focus + .govuk-checkboxes__label::before {
    border-width: 4px;

    // When colours are overridden, the yellow box-shadow becomes invisible
    // which means the focus state is less obvious. By adding a transparent
    // outline, which becomes solid (text-coloured) in that context, we ensure
    // the focus remains clearly visible.
    outline: $govuk-focus-width solid transparent;
    outline-offset: 1px;

    // When in an explicit forced-color mode, we can use the Highlight system
    // color for the outline to better match focus states of native controls
    @media screen and (forced-colors: active), (-ms-high-contrast: active) {
      outline-color: Highlight;
    }

    box-shadow: 0 0 0 $govuk-focus-width govuk-functional-colour(focus);
  }

  // Selected state
  .govuk-checkboxes__input:checked + .govuk-checkboxes__label::after {
    opacity: 1;
  }

  // Disabled state
  .govuk-checkboxes__input:disabled,
  .govuk-checkboxes__input:disabled + .govuk-checkboxes__label {
    cursor: not-allowed;
  }

  .govuk-checkboxes__input:disabled + .govuk-checkboxes__label,
  .govuk-checkboxes__input:disabled ~ .govuk-hint {
    opacity: 0.5;
  }

  // =========================================================
  // Dividers ('or')
  // =========================================================

  .govuk-checkboxes__divider {
    @include govuk-font($size: 19);
    @include govuk-text-colour;
    $govuk-divider-size: $govuk-checkboxes-size !default;
    width: $govuk-divider-size;
    margin-bottom: govuk-spacing(2);
    text-align: center;
  }

  // =========================================================
  // Conditional reveals
  // =========================================================

  // The narrow border is used in the conditional reveals because the border has
  // to be an even number in order to be centred under the 40px checkbox or radio.
  $conditional-border-width: $govuk-border-width-narrow;
  // Calculate the amount of padding needed to keep the border centered against the checkbox.
  $conditional-border-padding: ($govuk-touch-target-size / 2) - ($conditional-border-width / 2);
  // Move the border centered with the checkbox
  $conditional-margin-left: $conditional-border-padding;
  // Move the contents of the conditional inline with the label
  $conditional-padding-left: $conditional-border-padding + $govuk-checkboxes-label-padding-left-right;

  .govuk-checkboxes__conditional {
    @include govuk-responsive-margin(4, "bottom");
    margin-left: $conditional-margin-left;
    padding-left: $conditional-padding-left;
    border-left: $conditional-border-width solid govuk-functional-colour(border);

    .govuk-frontend-supported &--hidden {
      display: none;
    }

    & > :last-child {
      margin-bottom: 0;
    }
  }

  // =========================================================
  // Small checkboxes
  // =========================================================

  .govuk-checkboxes--small {
    $input-offset: ($govuk-touch-target-size - $govuk-small-checkboxes-size) / 2;

    .govuk-checkboxes__item {
      margin-bottom: 0;
    }

    // Shift the touch target into the left margin so that the visible edge of
    // the control is aligned
    //
    //   ┆What colours do you like?
    //  ┌┆───┐
    //  │┆[] │ Purple
    //  └┆▲──┘
    //  ▲┆└─ Check box pseudo element, aligned with margin
    //  └─── Touch target (invisible input), shifted into the margin
    .govuk-checkboxes__input {
      margin-left: $input-offset * -1;
    }

    .govuk-checkboxes__label {
      // Create a tiny space between the small checkbox hover state so that it
      // doesn't clash with the label
      padding-left: 1px;
    }

    // [ ] Check box
    //
    // Reduce the size of the check box [1], vertically center it within the
    // touch target [2]
    // Left here is 0 because we've shifted the input into the left margin
    .govuk-checkboxes__label::before {
      top: $input-offset; // 2
      left: 0;
      width: $govuk-small-checkboxes-size; // 1
      height: $govuk-small-checkboxes-size; // 1
    }

    // ✔ Check mark
    //
    // Reduce the size of the check mark and re-align within the checkbox
    .govuk-checkboxes__label::after {
      top: 17px;

      // Horizontal position is just the normal sized left value accounting for
      // the new width of the smaller checkbox
      left: (16px - $govuk-checkbox-check-horizontal-position);
      width: 12px;
      height: 6.5px;
      border-width: 0 0 3px 3px;
    }

    // Fix position of hint with small checkboxes
    //
    // Do not use hints with small checkboxes – because they're within the input
    // wrapper they trigger the hover state, but clicking them doesn't actually
    // activate the control.
    //
    // (If you do use them, they won't look completely broken... but seriously,
    // don't use them)
    .govuk-checkboxes__hint {
      padding-left: ($govuk-small-checkboxes-size + $input-offset);
    }

    // Align conditional reveals with small checkboxes
    .govuk-checkboxes__conditional {
      $margin-left: ($govuk-small-checkboxes-size / 2) - ($conditional-border-width / 2);
      margin-left: $margin-left;
      padding-left: ($govuk-touch-target-size - $input-offset) - ($margin-left + $conditional-border-width);
    }

    .govuk-checkboxes__divider {
      width: $govuk-small-checkboxes-size;
      margin-bottom: govuk-spacing(1);
    }

    // Hover state for small checkboxes.
    //
    // We use a hover state for small checkboxes because the touch target size
    // is so much larger than their visible size, and so we need to provide
    // feedback to the user as to which checkbox they will select when their
    // cursor is outside of the visible area.
    .govuk-checkboxes__item:hover .govuk-checkboxes__input:not(:disabled) + .govuk-checkboxes__label::before {
      // Forced colours modes tend to ignore box-shadow.
      // Apply an outline for those modes to use instead.
      outline: $govuk-focus-width dashed transparent;
      outline-offset: 1px;
      box-shadow: 0 0 0 $govuk-hover-width govuk-functional-colour(hover);
    }

    // Because we've overridden the border-shadow provided by the focus state,
    // we need to redefine that too.
    //
    // We use two box shadows, one that restores the original focus state [1]
    // and another that then applies the hover state [2].
    .govuk-checkboxes__item:hover .govuk-checkboxes__input:focus + .govuk-checkboxes__label::before {
      // Set different HCM colour when we have both hover/focus applied at once
      @media screen and (forced-colors: active), (-ms-high-contrast: active) {
        outline-color: Highlight;
      }
      // prettier-ignore
      box-shadow:
        0 0 0 $govuk-focus-width govuk-functional-colour(focus), // 1
        0 0 0 $govuk-hover-width govuk-functional-colour(hover); // 2
    }

    // For devices that explicitly don't support hover, don't provide a hover
    // state (e.g. on touch devices like iOS).
    //
    // We can't use `@media (hover: hover)` because we wouldn't get the hover
    // state in browsers that don't support `@media (hover)` (like Internet
    // Explorer) – so we have to 'undo' the hover state instead.
    @media (hover: none), (pointer: coarse) {
      .govuk-checkboxes__item:hover .govuk-checkboxes__input:not(:disabled) + .govuk-checkboxes__label::before {
        box-shadow: initial;
      }

      .govuk-checkboxes__item:hover .govuk-checkboxes__input:focus + .govuk-checkboxes__label::before {
        box-shadow: 0 0 0 $govuk-focus-width govuk-functional-colour(focus);
      }
    }
  }
}
